<template>
  <div class="film-list">
    <van-list
      v-model="loading"
      :finished="finished"
      :immediate-check="false"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ul class="nowPlayingList">
        <li v-for="item in filmList" :key="item.filmId" @click="handleClick(item.filmId)">
          <div class="nowPlayingFilm-img"><img :src="item.poster" alt="海报"></div>
          <div class="nowPlayingFilm-info">
            <div class="info-col">
              <span class="name">{{ item.name }}</span>
              <span class="filmType">{{ item.filmType.name }}</span>
            </div>
            <div class="info-col">
              <span class="label">观众评分：</span>
              <span class="grade">{{ item.grade }}</span>
            </div>
            <div class="label info-col">主演：{{ item.actors | actorsFilter }}</div>
            <div class="info-col">
              <span class="label">{{ item.nation }} | {{ item.runtime }}分钟</span>
            </div>
          </div>
          <div class="nowPlayingFilm-buy">购票</div>
        </li>
      </ul>
    </van-list>
  </div>
</template>

<script>
import Vue from 'vue'
import http from '@/util/http'
import { List } from 'vant'

Vue.use(List)

export default {
  name: 'NowPlaying',
  data () {
    return {
      filmList: [],
      loading: false,
      finished: false,
      currentPage: 1,
      total: 0
    }
  },
  mounted () {
    http({
      url: '/gateway?cityId=310100&pageNum=1&pageSize=10&type=1&k=7899293',
      headers: {
        'X-Host': 'mall.film-ticket.film.list'
      }
    }).then(res => {
      this.filmList = res.data.data.films
      this.total = res.data.data.total
    })
  },
  methods: {
    onLoad () {
      // 1. Ajax请求新页面的数据
      // 2. 将新数据合并到老数据中
      // 3. this.loading = false
      if (this.filmList.length === this.total && this.filmList.length !== 0) {
        this.finished = true
        return
      }
      this.currentPage++
      http({
        url: `/gateway?cityId=${this.$store.state.cityId}&pageNum=${this.currentPage}&pageSize=10&type=1&k=7899293`,
        headers: {
          'X-Host': 'mall.film-ticket.film.list'
        }
      }).then(res => {
        this.filmList = [...this.filmList, ...res.data.data.films]
        this.loading = false
      })
    },
    handleClick (id) {
      // 1.路径
      this.$router.push(`/detail/${id}`) // 编程式导航

      // 2.路由名字
      // this.$router.push({
      //   name: 'Detail',
      //   params: {
      //     id: id
      //   }
      // })

      // 3.query方式跳转
      // this.$router.push(`/detail?id=${id}`)
    }
  }
}
</script>

<style lang="scss" scoped>
.film-list {
  background-color: #ffffff;

}

.nowPlayingList {
  margin-left: 4vw;

  li::after {
    position: absolute;
    border-bottom: 1px solid #ededed;
    height: 32vw;
    content: " ";
    width: 92%;
  }

  li {
    overflow: hidden;
    padding: 4vw 4vw 4vw 0;
    height: 94px;
    display: flex;
    align-items: center;

    .info-col {
      margin: 0.5vw 0;
    }

    .nowPlayingFilm-info {
      padding: 0 10px;
      width: 60%;

      .label {
        color: #797d82;
        font-size: 13px;
        width: 100%;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }

      .grade {
        color: #ffb232;
        font-size: 14px;
      }
    }

    .nowPlayingFilm-img {
      background: rgb(249, 249, 249);

      img {
        width: 17.6vw;
        height: 25.07vw;
      }
    }

    .nowPlayingFilm-buy {
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid #ff5f16;
      width: 50px;
      height: 25px;
      color: #ff5f16;
      font-size: 13px;
      border-radius: 2px;
    }
  }
}

.name {
  margin-right: 5px;
}

.filmType {
  background-color: #d2d6dc;
  border-radius: 2px;
  font-size: 9px;
  color: #ffffff;
  height: 14px;
  line-height: 14px;
  padding: 0 2px;
  font-family: system-ui, serif;
}
</style>
